package com.example.service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.service.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private DownloadService downloadService;
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 设置按钮的点击事件监听器
        binding.btnBind.setOnClickListener(this);
        binding.btnUnbind.setOnClickListener(this);
        binding.btnDownload.setOnClickListener(this);
        // 设置前台服务按钮的监听器
        binding.btnStartForeground.setOnClickListener(this);
    }

    // 服务连接接口
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i("MainActivity", "onServiceDisconnected()意外销毁被调用");
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder iBinder) {
            Log.i("MainActivity", "onServiceConnected()");
            downloadService = ((DownloadService.MyBinder) iBinder).getService();
            downloadService.setOnProgressListener(new DownloadService.OnProgressListener() {
                @Override
                public void onProgress(int progress) {
                    binding.progressBar.setProgress(progress);
                    // 下载完成则移除进度条
                    if (progress == 100) {
                        binding.progressBar.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
                                binding.progressBar.setProgress(0);
                                binding.progressBar.setVisibility(View.GONE);
                            }
                        });
                    }
                }
            });
        }
    };

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        if (id == R.id.btn_bind) {
            // 绑定服务
            Intent intent = new Intent(this, DownloadService.class);
            bindService(intent, connection, BIND_AUTO_CREATE);
        } else if (id == R.id.btn_download) {
            // 下载
            if (downloadService != null) {
                downloadService.startDownload();
                binding.progressBar.setVisibility(View.VISIBLE);
            } else {
                Toast.makeText(this, "先绑定服务", Toast.LENGTH_SHORT).show();
            }
        } else if (id == R.id.btn_unbind) {
            // 解绑服务
            unbindService(connection);
            downloadService = null;
        } else if (id == R.id.btn_start_foreground) {
            Intent intent = new Intent(this, ForegroundService.class);
            // Android 8.0使用startForegroundService启动前台服务
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(intent);
            } else {
                startService(intent);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            unbindService(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}